package xyz.yuanjilu.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.cybermkd.mongo.kit.MongoQuery;
import com.cybermkd.mongo.kit.page.MongoPage;
import com.cybermkd.mongo.kit.page.MongoPaginate;

import xyz.yuanjilu.service.TalentService;

public class TalentServiceImpl extends BaseMongoServiceImpl implements TalentService{
	/*@Value("${mongo.dbname}")
	private String dataBase;
	@Override
	protected String getDataBase() {
		return dataBase;
	}*/

	@Override
	protected String getUse() {
		return "talent";
	}

    /**
     * 查询人才
     */
    @Override
    public JSONObject queryObject(String id) {

        // .ne("invalid", true)
        JSONObject result = query().byId(id).findOne();
        super.close();

        return result;
    }

    /**
     * 查询人才列表
     */
    @Override
    public MongoPage queryList(int page, int limit, Map<String, Object> search) {

        // 查询条件 .ne("invalid", true)
        MongoQuery query = query();
        if (search != null && !search.isEmpty()) {
            for (Map.Entry<String, Object> entity : search.entrySet()) {
                query = query.like(entity.getKey(), (String) entity.getValue());
            }
        }

        // 分页对象
        MongoPaginate paginate = new MongoPaginate(query, limit, page);
        MongoPage result = paginate.find();
        super.close();

        return result;
    }

    @Override
    public List<JSONObject> queryList(Set<String> ids, Map<String, Object> search) {

        MongoQuery query = query();
        if (ids != null && !ids.isEmpty()) {
            List<MongoQuery> idsQueries = new ArrayList<>();
            for (String id : ids) {
                idsQueries.add(new MongoQuery().byId(id));
            }
            query = query.orQuery(idsQueries);
        }

        if (search != null && !search.isEmpty()) {
            for (Map.Entry<String, Object> entity : search.entrySet()) {
                query = query.like(entity.getKey(), (String) entity.getValue());
            }
        }

        List<JSONObject> list = query.find();
        super.close();

        return list;
    }

    @Override
    public JSONObject saveWithCheck(Map<String, Object> talent) {

        JSONObject json = (JSONObject) JSONObject.toJSON(talent);
        JSONObject baseInfo = json.getJSONObject("baseInfo");
        String phoneNumber = baseInfo.getString("phoneNumber");

        // 手机号相同
        JSONObject resume = query().eq("baseInfo.phoneNumber", phoneNumber).ne("invalid", true).findOne();
        super.close();
        if (resume != null) {
            resume.put("exists", "1");
        } else {
            resume = new JSONObject();
            resume.put("exists", "0");
        }

        return resume;
    }

    @Override
    public Long update(Map entity) {

        String id = (String) entity.get("_id");
        long num = query().byId(id).replace(entity);
        super.close();

        return num;
    }

}

